package fu.capstone.hanoicityguide.dataaccess.webservice;

import java.io.IOException;
import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalFloat;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.util.Log;
import android.widget.TextView;
import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.business.model.DBVersion;
import fu.capstone.hanoicityguide.common.utility.DAOUtility;
import fu.capstone.hanoicityguide.common.utility.InternetSetting;
import fu.capstone.hanoicityguide.dataaccess.base.BaseDAO;
import fu.capstone.hanoicityguide.dataaccess.base.BaseDAOImpl;
import fu.capstone.hanoicityguide.presentation.activity.MainActivity;
import fu.capstone.hanoicityguide.presentation.activity.SplashActivity;

public class WebServiceDAOImpl extends AsyncTask<Void, String, Boolean>
		implements WebServiceDAO {

	// variable
	private SoapObject soapArray;
	private SoapPrimitive soapPrimitive;
	private Context mContext;
	private boolean checkUpdate;

	private TextView txtStatus;

	// constructor
	public WebServiceDAOImpl(Context ctx, TextView txtStatus) {
		this.mContext = ctx;
		this.txtStatus = txtStatus;

	}

	// init webservice connection
	public SoapObject initWebService(String methodName) throws IOException,
			XmlPullParserException {
		SoapObject soapArray = null;
		// init request
		try {
			SoapObject request = new SoapObject(WebServiceDAO.NAMESPACE,
					methodName);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.setOutputSoapObject(request);

			MarshalFloat marshal = new MarshalFloat();
			marshal.register(envelope);

			HttpTransportSE httpTransport = new HttpTransportSE(URL);
			httpTransport.call(WebServiceDAO.NAMESPACE + methodName, envelope);
			soapArray = (SoapObject) envelope.getResponse();
		} catch (IOException e) {
			Log.v("Init Webservice : ", e.toString());
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return soapArray;
	}

	// init webservice connection
	public SoapPrimitive initWebServicePrimitive(String methodName)
			throws IOException, XmlPullParserException {
		SoapPrimitive soapPrimitive = null;
		// init request
		try {
			SoapObject request = new SoapObject(WebServiceDAO.NAMESPACE,
					methodName);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.setOutputSoapObject(request);
			MarshalFloat marshal = new MarshalFloat();
			marshal.register(envelope);
			HttpTransportSE httpTransport = new HttpTransportSE(URL);
			httpTransport.call(WebServiceDAO.NAMESPACE + methodName, envelope);
			soapPrimitive = (SoapPrimitive) envelope.getResponse();
		} catch (Exception e) {
			Log.v("Init Webservice Primitive : ", e.toString());
		}
		return soapPrimitive;
	}

	// get list Type from service
	@Override
	public boolean setListType() {
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				int typeId = Integer.parseInt(soapItem.getProperty("Id")
						.toString().trim());
				int categoryId = Integer.parseInt(soapItem
						.getProperty("CategoryId").toString().trim());
				String typeName = soapItem.getProperty("TypeName").toString()
						.trim();

				dao.insertType(typeId, categoryId, typeName);
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			Log.v("setListType : ", e.toString());
		}
		return checkSet;
	}

	@Override
	public boolean setListCategory() {
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				String categoryId = soapItem.getProperty("Id").toString()
						.trim();
				String categoryName = soapItem.getProperty("CategoryName")
						.toString().trim();

				dao.insertCategory(Integer.parseInt(categoryId), categoryName);
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public boolean setListClass() {
		boolean checkSet = false;
		try {

			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				String classId = soapItem.getProperty("Id").toString().trim();
				String className = soapItem.getProperty("ClassName").toString()
						.trim();

				dao.insertClass(Integer.parseInt(classId), className);
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public boolean setListDBVersion() {
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				String dbVersionId = soapItem.getProperty("Id").toString()
						.trim();
				String dbVersionName = soapItem.getProperty("VersionName")
						.toString().trim();
				String dbUpdateStatus = soapItem.getProperty("UpdateStatus")
						.toString().trim();
				dao.insertDBVersion(Integer.parseInt(dbVersionId),
						dbVersionName, dbUpdateStatus);
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public boolean setListGeolocation() {
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				String geoId = soapItem.getProperty("Id").toString();
				String geoLong = soapItem.getProperty("Longitude").toString();
				String geoLat = soapItem.getProperty("Latitude").toString();

				dao.insertGeolocation(Integer.parseInt(geoId),
						Double.parseDouble(geoLong), Double.parseDouble(geoLat));
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public boolean setListRating() {
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				String ratingId = soapItem.getProperty("Id").toString();
				String ratingValue = soapItem.getProperty("RatingValue")
						.toString();

				dao.insertRating(Integer.parseInt(ratingId),
						Integer.parseInt(ratingValue));
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public boolean setListPlace() {
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				String placeId = soapItem.getProperty("Id").toString().trim();
				String placeName = soapItem.getProperty("Name").toString()
						.trim();
				String placeGeoId = soapItem.getProperty("GeoId").toString()
						.trim();
				String placeCategoryId = soapItem.getProperty("CategoryId")
						.toString().trim();
				String placeTypeId = soapItem.getProperty("TypeId").toString()
						.trim();
				String placeAlbumImageId = soapItem.getProperty("AlbumImageId")
						.toString().trim();
				String placeRatingId = soapItem.getProperty("RatingId")
						.toString().trim();
				String placeClassId;

				if (soapItem.getProperty("ClassId") != null) {
					placeClassId = soapItem.getProperty("ClassId").toString()
							.trim();
				} else {
					placeClassId = "0";
				}

				String placePrice;

				if (soapItem.getProperty("Price") != null) {
					placePrice = soapItem.getProperty("Price").toString()
							.trim();
				} else {
					placePrice = "0";
				}

				String placeAddress = soapItem.getProperty("Address")
						.toString().trim();
				String placeOpenTime;

				if (soapItem.getProperty("OpenTime") != null) {
					placeOpenTime = soapItem.getProperty("OpenTime").toString()
							.trim();
				} else {
					placeOpenTime = "";
				}

				String placeCloseTime;

				if (soapItem.getProperty("CloseTime") != null) {
					placeCloseTime = soapItem.getProperty("CloseTime")
							.toString().trim();
				} else {
					placeCloseTime = "";
				}

				String placePhoneNumber = "";

				if (soapItem.getProperty("PhoneNumber") != null) {
					placePhoneNumber = soapItem.getProperty("PhoneNumber")
							.toString().trim();
				} else {
					placePhoneNumber = "";
				}

				String placeWebsite = "";

				if (soapItem.getProperty("Website") != null) {
					placeWebsite = soapItem.getProperty("Website").toString()
							.trim();
				} else {
					placeWebsite = "";
				}

				String placeDescription = soapItem.getProperty("Description")
						.toString().trim();
				int dBVersionId = Integer.parseInt(soapItem
						.getProperty("DBVersionId").toString().trim());

				dao.insertPlace(Integer.parseInt(placeId), placeName,
						Integer.parseInt(placeGeoId),
						Integer.parseInt(placeCategoryId),
						Integer.parseInt(placeTypeId),
						Integer.parseInt(placeAlbumImageId),
						Integer.parseInt(placeRatingId),
						Integer.parseInt(placeClassId),
						Float.parseFloat(placePrice), placeDescription,
						placeAddress, placePhoneNumber, placeWebsite,
						placeOpenTime, placeCloseTime, dBVersionId);
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public boolean setListAlbumImage() {
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			ArrayList<String> arrImgPath;
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				arrImgPath = new ArrayList<String>();
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				String albumId = soapItem.getProperty("Id").toString();
				String iconPath = soapItem.getProperty("IconPath").toString();
				arrImgPath.add(soapItem.getProperty("ImagePath1").toString());
				arrImgPath.add(soapItem.getProperty("ImagePath2").toString());
				arrImgPath.add(soapItem.getProperty("ImagePath3").toString());
				arrImgPath.add(soapItem.getProperty("ImagePath4").toString());
				arrImgPath.add(soapItem.getProperty("ImagePath5").toString());

				dao.insertAlbumImage(Integer.parseInt(albumId), iconPath,
						arrImgPath);
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public boolean setListFood() {
		// TODO Auto-generated method stub
		boolean checkSet = false;

		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				String foodID = soapItem.getProperty("Id").toString().trim();
				String foodName = soapItem.getProperty("Name").toString()
						.trim();
				String foodDesc = soapItem.getProperty("Description")
						.toString().trim();
				String ratingID = soapItem.getProperty("RatingId").toString()
						.trim();
				String albumImageId = soapItem.getProperty("AlbumImageId")
						.toString().trim();
				String geoId = soapItem.getProperty("GeoId").toString().trim();
				String price = soapItem.getProperty("Price").toString().trim();
				String address = soapItem.getProperty("Address").toString()
						.trim();
				String phoneNumber = soapItem.getProperty("PhoneNumber")
						.toString().trim();

				dao.insertFood(Integer.parseInt(foodID), foodName, foodDesc,
						Integer.parseInt(ratingID),
						Integer.parseInt(albumImageId),
						Integer.parseInt(geoId), Float.parseFloat(price),
						address, phoneNumber);
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public boolean setListHoliday() {
		// TODO Auto-generated method stub
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				String holidayID = soapItem.getProperty("Id").toString().trim();
				String holidayName = soapItem.getProperty("Name").toString()
						.trim();
				String holidayDesc = soapItem.getProperty("Description")
						.toString().trim();
				String albumImageId = soapItem.getProperty("AlbumImageId")
						.toString().trim();

				dao.insertHoliday(Integer.parseInt(holidayID), holidayName,
						holidayDesc, Integer.parseInt(albumImageId));
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}

		return checkSet;
	}

	@Override
	public boolean setListTransportCategory() {
		// get TransportCategory from webservice and insert into client database
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				String transportCategoryID = soapItem.getProperty("Id")
						.toString().trim();
				String transportCategoryName = soapItem
						.getProperty("TransportName").toString().trim();

				dao.insertTransportCategory(
						Integer.parseInt(transportCategoryID),
						transportCategoryName);
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public boolean setListTransport() {
		// insertTransport
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				String transportID = soapItem.getProperty("Id").toString()
						.trim();
				String transportName = soapItem.getProperty("Name").toString()
						.trim();
				String transportCategoryID = soapItem
						.getProperty("TransportCategoryId").toString().trim();
				String iconPath = soapItem.getProperty("IconPath").toString()
						.trim();
				String firstLeg = "";
				if (soapItem.getProperty("FirstLeg") != null) {
					firstLeg = soapItem.getProperty("FirstLeg").toString()
							.trim();
				} else {
					firstLeg = "";
				}
				String secondLeg = "";
				if (soapItem.getProperty("LastLeg") != null) {
					secondLeg = soapItem.getProperty("LastLeg").toString()
							.trim();
				} else {
					secondLeg = "";
				}

				String price = "";
				if (soapItem.getProperty("Price") != null) {
					price = soapItem.getProperty("Price").toString().trim();
				} else {
					price = "";
				}

				String website = "";
				if (soapItem.getProperty("Website") != null) {
					website = soapItem.getProperty("Website").toString().trim();
				} else {
					website = "";
				}

				String phoneNumber = "";
				if (soapItem.getProperty("PhoneNumber") != null) {
					phoneNumber = soapItem.getProperty("PhoneNumber")
							.toString().trim();
				} else {
					phoneNumber = "";
				}

				dao.insertTransport(Integer.parseInt(transportID),
						Integer.parseInt(transportCategoryID), transportName,
						firstLeg, secondLeg, price, phoneNumber, website,
						iconPath);
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public boolean setListCommonDialogue() {
		// TODO Auto-generated method stub
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				String commonDialogueID = soapItem.getProperty("Id").toString()
						.trim();
				String vietnameseContent = soapItem
						.getProperty("VietnameseSentence").toString().trim();
				String englishContent = soapItem.getProperty("EnglishSentence")
						.toString().trim();
				String audioPath = soapItem.getProperty("AudioPath").toString()
						.trim();

				dao.insertCommonDialogue(Integer.parseInt(commonDialogueID),
						vietnameseContent, englishContent, audioPath);
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public boolean setHome() {
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				String homeId = soapItem.getProperty("Id").toString().trim();
				String overview = soapItem.getProperty("Overview").toString()
						.trim();
				String history = soapItem.getProperty("History").toString()
						.trim();
				String albumImageId = soapItem.getProperty("AlbumImageId")
						.toString().trim();

				dao.insertHome(Integer.parseInt(homeId), overview, history,
						Integer.parseInt(albumImageId));
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public boolean setListItinerary() {
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				int itineraryId = Integer.parseInt(soapItem.getProperty("Id")
						.toString().trim());
				String itineraryName = soapItem.getProperty("ItineraryName")
						.toString().trim();
				int ratingId = Integer.parseInt(soapItem
						.getProperty("RatingId").toString().trim());
				int albumImageId = Integer.parseInt(soapItem
						.getProperty("AlbumImageId").toString().trim());
				int dbVersionId = Integer.parseInt(soapItem
						.getProperty("DBVersionId").toString().trim());

				dao.insertItinerary(itineraryId, itineraryName, ratingId,
						albumImageId, dbVersionId);
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public boolean setListItineraryDetail() {
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				int itineraryId = Integer.parseInt(soapItem
						.getProperty("ItineraryId").toString().trim());
				int placeId = Integer.parseInt(soapItem.getProperty("PlaceId")
						.toString().trim());
				int placeOrder = Integer.parseInt(soapItem
						.getProperty("PlaceOrder").toString().trim());
				int dBVersionId = Integer.parseInt(soapItem
						.getProperty("DBVersionId").toString().trim());

				dao.insertItineraryDetail(itineraryId, placeId, placeOrder,
						dBVersionId);
			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public boolean setListTour() {
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				int id = Integer.parseInt(soapItem.getProperty("Id").toString()
						.trim());
				String name = soapItem.getProperty("Name").toString().trim();
				int providerId = Integer.parseInt(soapItem
						.getProperty("ProviderId").toString().trim());
				int ratingId = Integer.parseInt(soapItem
						.getProperty("RatingId").toString().trim());
				int albumImageId = Integer.parseInt(soapItem
						.getProperty("AlbumImageId").toString().trim());
				float price = Float.parseFloat(soapItem.getProperty("Price")
						.toString().trim());
				float duration = Float.parseFloat(soapItem
						.getProperty("Duration").toString().trim());
				String description = soapItem.getProperty("Description")
						.toString().trim();
				int dbVersionId = Integer.parseInt(soapItem
						.getProperty("DBVersionId").toString().trim());

				dao.insertTour(id, name, providerId, ratingId, albumImageId,
						price, duration, description, dbVersionId);

			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public boolean setListTourProvider() {
		boolean checkSet = false;
		try {
			BaseDAO dao = new BaseDAOImpl(mContext);
			for (int i = 0; i < soapArray.getPropertyCount(); i++) {
				SoapObject soapItem = (SoapObject) soapArray.getProperty(i);
				int id = Integer.parseInt(soapItem.getProperty("Id").toString()
						.trim());
				String name = soapItem.getProperty("Name").toString().trim();
				String iconPath = soapItem.getProperty("IconPath").toString()
						.trim();
				String address = soapItem.getProperty("Address").toString()
						.trim();
				String website = soapItem.getProperty("Website").toString()
						.trim();
				String phoneNumber = soapItem.getProperty("PhoneNumber")
						.toString().trim();
				int dbVersionId = Integer.parseInt(soapItem
						.getProperty("DBVersionId").toString().trim());

				dao.insertTourProvider(id, name, iconPath, address, website,
						phoneNumber, dbVersionId);

			}
			checkSet = true;
		} catch (Exception e) {
			checkSet = false;
			e.printStackTrace();
		}
		return checkSet;
	}

	@Override
	public int getCountAlbumImage() {
		// get count from AlbumImage table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_ALBUM_IMAGE);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountCategory() {
		// get count from Category table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_CATEGORY);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountClass() {
		// get count from Class table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_CLASS);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountCommonDialogue() {
		// get count from Common Dialogue table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_COMMON_DIALOGUE);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountDBVersion() {
		// get count from DBVersion table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_DBVERSION);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountFood() {
		// get count from Food table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_FOOD);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountGeolocation() {
		// get count from Geolocation table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_GEOLOCATION);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountHoliday() {
		// get count from Holiday table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_HOLIDAY);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountHome() {
		// get count from Home table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_HOME);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountItinerary() {
		// get count from Itinerary table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_ITINERARY);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountItineraryDetail() {
		// get count from ItineraryDetail table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_ITINERARYDETAIL);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountPlace() {
		// get count from Place table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_PLACE);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountRating() {
		// get count from Rating table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_RATING);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountTour() {
		// get count from Tour table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_TOUR);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountTourProvider() {
		// get count from TourProvider table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_TOURPROVIDER);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountTransport() {
		// get count from Transport table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_TRANSPORT);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountTransportCategory() {
		// get count from TransportCategory table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_TRANSPORT_CATEGORY);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int getCountType() {
		// get count from Type table from Webservice
		try {
			initWebServicePrimitive(METHOD_COUNT_TYPE);
			return Integer.parseInt(soapPrimitive.toString().trim());
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		} catch (XmlPullParserException e) {
			e.printStackTrace();
			return -1;
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public boolean checkDBVersion() {
		boolean checkVersion = false;
		// get lastest version from DBVersionId table from Webservice
		int wsLastestVersion = Integer
				.parseInt(soapPrimitive.toString().trim());
		BaseDAO dao = new BaseDAOImpl(mContext);
		if (wsLastestVersion > dao.getLastestDBVersionId()) {
			return true;
		} else {
			return false;
		}

	}

	// override from AsyncTask class
	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		publishProgress("Initializing...");

	}

	@Override
	protected Boolean doInBackground(Void... params) {
		// TODO Auto-generated method stub
		DBVersion dbVer = new DBVersion();
		DAOUtility daoUtil = new DAOUtility();
		InternetSetting itSetting = new InternetSetting(mContext);
		BaseDAO dao = new BaseDAOImpl(mContext);
		soapArray = null;
		// soapPrimitive = null;

		// if database is existed
		if (daoUtil.checkExisted()) {
			// if has no internet connection
			if (!itSetting.getConnectivityStatus(mContext)) {
				checkUpdate = true;
				SystemClock.sleep(2000);
			}
			// if has internet connection
			else {
				// try to check dbversion

				boolean checkBackup = false;
				boolean checkDeleteDB = false;
				boolean checkDBVersionId = false;
				try {
					soapPrimitive = initWebServicePrimitive(METHOD_GET_LASTESTVERSIONID);
					checkDBVersionId = checkDBVersion();
					// get lastest version of dbversion
					publishProgress("Check for updating...");

					// if confirm for updating
					if (checkDBVersionId) {
						publishProgress("Updating...");
						// backup database
						checkBackup = dao.backupDatabase();
						checkDeleteDB = dao.deleteDatabase();

						publishProgress("Downloading 'Type' data...");
						soapArray = initWebService(METHOD_GET_TYPE);
						if (soapArray != null) {
							setListType();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;

						}

						publishProgress("Downloading 'Category' data...");
						soapArray = initWebService(METHOD_GET_CATEGORY);
						if (soapArray != null) {
							setListCategory();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'Class' data...");
						soapArray = initWebService(METHOD_GET_CLASS);
						if (soapArray != null) {
							setListClass();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'Geolocation' data...");
						soapArray = initWebService(METHOD_GET_GEOLOCATION);
						if (soapArray != null) {
							setListGeolocation();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'Rating' data...");
						soapArray = initWebService(METHOD_GET_RATING);
						if (soapArray != null) {
							setListRating();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'AlbumImage' data...");
						soapArray = initWebService(METHOD_GET_ALBUM_IMAGE);
						if (soapArray != null) {
							setListAlbumImage();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'Place' data...");
						soapArray = initWebService(METHOD_GET_PLACE);
						if (soapArray != null) {
							setListPlace();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'Food' data...");
						soapArray = initWebService(METHOD_GET_FOOD);
						if (soapArray != null) {
							setListFood();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'Holiday' data...");
						soapArray = initWebService(METHOD_GET_HOLIDAY);
						if (soapArray != null) {
							setListHoliday();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'TransportCategory' data...");
						soapArray = initWebService(METHOD_GET_TRANSPORT_CATEGORY);
						if (soapArray != null) {
							setListTransportCategory();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'Transport' data...");
						soapArray = initWebService(METHOD_GET_TRANSPORT);
						if (soapArray != null) {
							setListTransport();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'CommonDialogue' data...");
						soapArray = initWebService(METHOD_GET_COMMON_DIALOGUE);
						if (soapArray != null) {
							setListCommonDialogue();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'Itinerary' data...");
						soapArray = initWebService(METHOD_GET_ITINERARY);
						if (soapArray != null) {
							setListItinerary();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'ItineraryDetail' data...");
						soapArray = initWebService(METHOD_GET_ITINERARYDETAIL);
						if (soapArray != null) {
							setListItineraryDetail();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'TourProvider' data...");
						soapArray = initWebService(METHOD_GET_TOURPROVIDER);
						if (soapArray != null) {
							setListTourProvider();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'Tour' data...");
						soapArray = initWebService(METHOD_GET_TOUR);
						if (soapArray != null) {
							setListTour();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'Home' data...");
						soapArray = initWebService(METHOD_GET_HOME);
						if (soapArray != null) {
							setHome();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						publishProgress("Downloading 'DBVersion' data...");
						soapArray = initWebService(METHOD_GET_DBVERSION);
						if (soapArray != null) {
							setListDBVersion();
						} else {
							checkUpdate = false;
							if (daoUtil.checkExisted()) {
								dao.deleteDatabase();
							}
							publishProgress("FALSE");
							return checkUpdate;
						}

						checkUpdate = true;
					} else {
						publishProgress("Done !");
						Intent i = new Intent(mContext, MainActivity.class);
						SystemClock.sleep(1000);
						mContext.startActivity(i);
						// close this activity
						((Activity) mContext).finish();
					}
				} catch (Exception e) {
					// restoring database when error occur
					publishProgress("Restoring database...");
					if (checkBackup) {
						if (dao.restoreDatabase()) {
							// delete temp database
							dao.deleteBackupDatabase();
						}
					}
					publishProgress("FALSE");
					checkUpdate = false;
				}
			}
		}
		// if database is not existed
		else {
			// if has no internet connection
			if (!itSetting.getConnectivityStatus(mContext)) {
				publishProgress("NOINTERNET");
				checkUpdate = false;
				return checkUpdate;
			}
			// if has internet connection
			else {
				try {
					publishProgress("Creating database...");

					publishProgress("Downloading 'Type' data...");
					soapArray = initWebService(METHOD_GET_TYPE);
					if (soapArray != null) {
						setListType();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;

					}

					publishProgress("Downloading 'Category' data...");
					soapArray = initWebService(METHOD_GET_CATEGORY);
					if (soapArray != null) {
						setListCategory();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'Class' data...");
					soapArray = initWebService(METHOD_GET_CLASS);
					if (soapArray != null) {
						setListClass();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'Geolocation' data...");
					soapArray = initWebService(METHOD_GET_GEOLOCATION);
					if (soapArray != null) {
						setListGeolocation();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'Rating' data...");
					soapArray = initWebService(METHOD_GET_RATING);
					if (soapArray != null) {
						setListRating();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'AlbumImage' data...");
					soapArray = initWebService(METHOD_GET_ALBUM_IMAGE);
					if (soapArray != null) {
						setListAlbumImage();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'Place' data...");
					soapArray = initWebService(METHOD_GET_PLACE);
					if (soapArray != null) {
						setListPlace();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'Food' data...");
					soapArray = initWebService(METHOD_GET_FOOD);
					if (soapArray != null) {
						setListFood();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'Holiday' data...");
					soapArray = initWebService(METHOD_GET_HOLIDAY);
					if (soapArray != null) {
						setListHoliday();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'TransportCategory' data...");
					soapArray = initWebService(METHOD_GET_TRANSPORT_CATEGORY);
					if (soapArray != null) {
						setListTransportCategory();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'Transport' data...");
					soapArray = initWebService(METHOD_GET_TRANSPORT);
					if (soapArray != null) {
						setListTransport();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'CommonDialogue' data...");
					soapArray = initWebService(METHOD_GET_COMMON_DIALOGUE);
					if (soapArray != null) {
						setListCommonDialogue();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'Itinerary' data...");
					soapArray = initWebService(METHOD_GET_ITINERARY);
					if (soapArray != null) {
						setListItinerary();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'ItineraryDetail' data...");
					soapArray = initWebService(METHOD_GET_ITINERARYDETAIL);
					if (soapArray != null) {
						setListItineraryDetail();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'TourProvider' data...");
					soapArray = initWebService(METHOD_GET_TOURPROVIDER);
					if (soapArray != null) {
						setListTourProvider();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'Tour' data...");
					soapArray = initWebService(METHOD_GET_TOUR);
					if (soapArray != null) {
						setListTour();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'Home' data...");
					soapArray = initWebService(METHOD_GET_HOME);
					if (soapArray != null) {
						setHome();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					publishProgress("Downloading 'DBVersion' data...");
					soapArray = initWebService(METHOD_GET_DBVERSION);
					if (soapArray != null) {
						setListDBVersion();
					} else {
						checkUpdate = false;
						if (daoUtil.checkExisted()) {
							dao.deleteDatabase();
						}
						publishProgress("FALSE");
						return checkUpdate;
					}

					checkUpdate = true;
				} catch (Exception e) {
					publishProgress("FALSE");
					if (daoUtil.checkExisted()) {
						dao.deleteDatabase();
					}

					checkUpdate = false;
				}
			}
		}
		SplashActivity.checkUpdate = checkUpdate;
		return checkUpdate;
	}

	@Override
	protected void onPostExecute(Boolean result) {
		// TODO Auto-generated method stub
		super.onPostExecute(result);
		if (checkUpdate) {
			publishProgress("Done !");
			Intent i = new Intent(mContext, MainActivity.class);
			mContext.startActivity(i);
			// close this activity
			((Activity) mContext).finish();
		}

	}

	@Override
	protected void onProgressUpdate(String... values) {
		// TODO Auto-generated method stub
		AlertDialog.Builder alertDialog;
		InternetSetting itSetting = new InternetSetting(mContext);
		if (values[0].toString().equals("FALSE")) {
			Log.v("FALSE", "FALSE");
			alertDialog = new AlertDialog.Builder(mContext);
			// Setting Dialog Title
			alertDialog.setTitle("Error !");
			alertDialog.setCancelable(false);
			// Setting Dialog Message
			alertDialog.setMessage("Do you want try again ?");
			// Setting Icon to Dialog
			alertDialog.setIcon(R.drawable.ic_action_directions);
			// Setting Positive "Yes" Button
			alertDialog.setPositiveButton("YES",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							// Write your code here to invoke YES event
							doInBackground(null);
						}
					});
			// Setting Negative "NO" Button
			alertDialog.setNegativeButton("NO",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							// Write your code here to invoke NO event
							((Activity) mContext).finish();
						}
					});
			alertDialog.show();
			alertDialog.setCancelable(false);
		} else if (values[0].equals("NOINTERNET")) {
			Log.v("NOINTERNET", "NOINTERNET");
			itSetting.showSettingsAlert();
		} else {
			txtStatus.setText(values[0]);
		}

	}

}
